package com.shanjige.chatgpt.chatgptweb.common;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.shanjige.chatgpt.chatgptweb.enums.ErrorEnum;
import java.io.Serializable;
import lombok.Data;

/**
 * @author qhong
 * @date 2023/2/7 19:03
 **/
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResultBase<T> implements Serializable {

    private static final long serialVersionUID = 7009143830260820446L;

    /**
     * 是否成功
     */
    private boolean isSuccess = false;

    /**
     * 异常信息
     */
    private String errorMsg = "";

    /**
     * 返回编码
     */
    private String errorCode = "";

    /**
     * 返回结果
     */
    private T value;

    public static <T> ResultBase<T> getErrorResult(ErrorEnum errorEnum) {
        ResultBase resultBase = new ResultBase();
        resultBase.setErrorCode(errorEnum.getErrorCode());
        resultBase.setErrorMsg(errorEnum.getErrorMessage());
        resultBase.setSuccess(false);
        return resultBase;
    }

    public static <T> ResultBase<T> getErrorResult(String errorMessage) {
        ResultBase resultBase = new ResultBase();
        resultBase.setErrorCode("-1");
        resultBase.setErrorMsg(errorMessage);
        return resultBase;
    }

    public static <T> ResultBase<T> getSuccessResult(T data) {
        ResultBase resultBase = new ResultBase();
        resultBase.setSuccess(true);
        resultBase.setErrorMsg(ErrorEnum.SUCCESS.getErrorMessage());
        resultBase.setErrorCode(ErrorEnum.SUCCESS.getErrorCode());
        resultBase.setValue(data);
        return resultBase;
    }

    public static <T> ResultBase<T> getSuccessResult() {
        ResultBase resultBase = new ResultBase();
        resultBase.setSuccess(true);
        resultBase.setErrorMsg(ErrorEnum.SUCCESS.getErrorMessage());
        resultBase.setErrorCode(ErrorEnum.SUCCESS.getErrorCode());
        resultBase.setValue(null);
        return resultBase;
    }

    public boolean isSuccess() {
        return this.isSuccess;
    }

    public String getErrorMsg() {
        return this.errorMsg;
    }

    public String getErrorCode() {
        return this.errorCode;
    }

    public T getValue() {
        return this.value;
    }

    public void setSuccess(final boolean isSuccess) {
        this.isSuccess = isSuccess;
    }

    public void setErrorMsg(final String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public void setErrorCode(final String errorCode) {
        this.errorCode = errorCode;
    }

    public void setValue(final T value) {
        this.value = value;
    }
}